<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row>
				<div class="flex justify-between w-full mb8">
					<el-space>
						<el-popover trigger="click" :width="360" placement="bottom-start">
							<el-form :inline="false" :model="state.queryForm" @keyup.enter="getDataList" ref="queryRef" label-width="90px" size="small">
								<el-row>
									<el-col :span="24" class="mb20">
										<el-form-item label="所属单位" prop="deptId">
											<el-tree-select
												v-model="state.queryForm.deptId"
												:data="parentData"
												:props="{ value: 'id', label: 'name', children: 'children', disabled: 'isLock' }"
												clearable
												filterable
												check-strictly
												:render-after-expand="false"
												placeholder="请选择所属单位"
												:teleported="false"
											/>
										</el-form-item>
									</el-col>
									<el-col :span="24" class="mb-4">
										<el-form-item label="单位限制" prop="deptLimit">
											<el-select v-model="state.queryForm.deptLimit" clearable placeholder="请选择单位限制" :teleported="false">
												<el-option :value="10" label="包含下级单位设备" />
												<el-option :value="20" label="仅本单位设备" />
											</el-select>
										</el-form-item>
									</el-col>
								</el-row>
							</el-form>

							<template #reference>
								<el-button link type="primary"
									>高级搜索<el-icon><ArrowDown /></el-icon>
								</el-button>
							</template>
						</el-popover>
						<el-input placeholder="姓名/电话/邮箱/单位名称" v-model="state.queryForm.name" />
						<el-button type="primary" @click="getDataList(true)">搜索</el-button>
						<el-button icon="refresh" @click="resetQuery()"></el-button>
					</el-space>
					<div>
						<el-button type="success" class="mr-2" @click="formDialogRef.openDialog()"> 新增</el-button>
						<!-- 更多操作 -->
						<el-popover :width="160">
							<template #reference>
								<el-button type="primary"
									>更多操作 <el-icon class="ArrowDown"><ArrowDown /></el-icon></el-button
							></template>
							<el-row>
								<el-col :span="24">
									<el-button
										class="ml-3"
										icon="edit-pen"
										text
										type="primary"
										:disabled="multiple || selectIds.length !== 1"
										v-auth="'platform_basicAlarmContacts_edit'"
										@click="formDialogRef.openDialog(selectIds[0])"
										>编辑接警人</el-button
									>
									<el-button
										text
										v-auth="'platform_basicAlarmContacts_del'"
										icon="DeleteFilled"
										type="danger"
										@click="handleDelete(selectIds)"
										:disabled="multiple"
										>删除接警人</el-button
									>
									<el-button
										icon="clock"
										text
										type="primary"
										:disabled="multiple || selectIds.length !== 1"
										@click="alarmTimeDialogRef.openDialog(selectObjs[0])"
										>接警时间段</el-button
									>
								</el-col>
							</el-row>
						</el-popover>
					</div>
				</div>
			</el-row>
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
				@selection-change="selectionChangHandle"
				@sort-change="sortChangeHandle"
			>
				<el-table-column type="selection" width="40" align="center" />
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="name" label="姓名" show-overflow-tooltip min-width="140" />
				<el-table-column prop="deptName" label="所属单位" show-overflow-tooltip width="150">
					<template #default="{ row }">
						<div class="text-left">
							<el-popover placement="left" title="所属单位层级关系" :width="200" trigger="click">
								<div v-for="(item, index) in level" :key="item.id">【{{ index + 1 }}】{{ item.name }}</div>
								<template #reference>
									<el-button type="primary" @click="getListAncestorById(row.deptId)" icon="view" size="small" link></el-button>
								</template>
							</el-popover>

							{{ row.deptName }}
						</div>
					</template>
				</el-table-column>
				<el-table-column prop="title" label="头衔" show-overflow-tooltip min-width="100" />
				<el-table-column prop="phone" label="电话" show-overflow-tooltip min-width="140" />
				<el-table-column prop="email" label="邮箱" show-overflow-tooltip min-width="140" />
				<el-table-column prop="createTime" label="创建日期" show-overflow-tooltip width="160" />
				<el-table-column prop="telephoneAlarm" label="通知方式" width="260">
					<template #default="{ row }">
						<!-- <el-checkbox v-model="row.telephoneAlarm" :true-label="20" :false-label="10" >电话</el-checkbox>
						<el-checkbox v-model="row.smsAlarm" :true-label="20" :false-label="10" >短信</el-checkbox>
						<el-checkbox v-model="row.officialAlarm" :true-label="20" :false-label="10" >公众号</el-checkbox> -->
						<div>
							<el-tag v-if="row.telephoneAlarm === 20">电话</el-tag>
							<el-tag v-if="row.smsAlarm === 20" class="ml-1">短信</el-tag>
							<el-tag v-if="row.officialAlarm === 20" class="ml-1">微信公众号</el-tag>
						</div>
					</template>
				</el-table-column>
				<el-table-column prop="installCount" label="绑定设备" show-overflow-tooltip width="90">
					<template #default="{ row }">
						<el-button @click="InstallDialogRef.openDialog(row.id)" link type="primary">{{ row.installCount }}</el-button>
					</template>
				</el-table-column>
				<el-table-column prop="remarks" label="备注" show-overflow-tooltip min-width="160" />
				<el-table-column label="操作" width="100" fixed="right">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="recordDialogRef.openDialog(scope.row.phone)">通知记录</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>

		<!-- 编辑、新增  -->
		<form-dialog ref="formDialogRef" @refresh="getDataList(false)" />
		<!-- 接警时段设置 -->
		<alarm-time-dialog ref="alarmTimeDialogRef" @refresh="getDataList(false)" />
		<!-- 通知记录 -->
		<record-dialog ref="recordDialogRef" />
		<!-- 接警人查看绑定安装点 -->
		<InstallDialog ref="InstallDialogRef" />
	</div>
</template>

<script setup lang="ts" name="systemBasicAlarmContacts">
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchList, delObjs } from '/@/api/installation/basicAlarmContacts';
import { useMessage, useMessageBox } from '/@/hooks/message';
import { deptTree } from '/@/api/admin/dept';
import { getListAncestor } from '/@/api/admin/dept';

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const AlarmTimeDialog = defineAsyncComponent(() => import('./components/alarmTime.vue'));
const RecordDialog = defineAsyncComponent(() => import('./components/record.vue'));
const InstallDialog = defineAsyncComponent(() => import('./components/installDialog.vue'));
// 定义查询字典

// 定义变量内容
const formDialogRef = ref();
const recordDialogRef = ref();
const alarmTimeDialogRef = ref();
const InstallDialogRef = ref();
const parentData = ref([]);
// 搜索变量
const queryRef = ref();
type levelItem = {
	id: string;
	name: string;
};
const level = ref<levelItem[]>([]);
// 多选变量
const selectObjs = ref([]) as any;
const selectIds = ref<string[]>([]);
const multiple = ref(true);

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {},
	pageList: fetchList,
	descs: ['create_time'],
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	// 清空搜索条件
	queryRef.value?.resetFields();
	// 清空多选
	selectObjs.value = [];
	selectIds.value = [];
	state.queryForm.name = '';
	getDataList();
};

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
	selectIds.value = objs.map(({ id }) => id);
	selectObjs.value = objs;
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm('删除不可恢复，确定还要继续吗？');
	} catch {
		return;
	}

	try {
		await delObjs(ids);
		getDataList();
		useMessage().success('删除成功');
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
const getDeptData = async () => {
	deptTree().then((res) => {
		parentData.value = res.data;
	});
};
getDeptData();
const getListAncestorById = (id: string) => {
	level.value = [];
	getListAncestor(id).then((res) => {
		level.value = res.data;
	});
};
</script>
